﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Text;

public partial class admin_studentManage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string student_id = "";
        string student_name = "";
        
        System.Data.DataTable table1 = new System.Data.DataTable();
        int count = 0;//总记录的条数
        try
        {
            //填充数据
            int page = Int32.Parse(Request.QueryString["page"].ToString().Trim());
            int limit = Int32.Parse(Request.QueryString["limit"].ToString().Trim());
            if (Request.QueryString["student_id"] != null)
            {
                 student_id = Request.QueryString["student_id"].ToString().Trim();
            }
            if (Request.QueryString["student_name"] != null)
            {
                student_name = Request.QueryString["student_name"].ToString().Trim();
            }
            
            string sqlGetDataCount = "select count(*) from tb_student as a inner join tb_major as b on a.major_id=b.major_id "
                    +"inner join tb_college as c on b.college_id = c.college_id ";
            List<string> wheres = new List<string>();

            if (student_id != "")
            {
                wheres.Add("student_id like '%" + student_id + "%'");
            }
            if (student_name != "")
            {
                wheres.Add("student_name like '%" + student_name + "%'");
            }
            string wh="";
            if(wheres.Count > 0)
            {
                wh = string.Join(" and ", wheres.ToArray());
                sqlGetDataCount = sqlGetDataCount + " where " +wh;
            }
            count = Int32.Parse(SqlServer.ExecuteScalar(sqlGetDataCount));
            int page_temp = page;
            int limit_temp = limit;
            if (count < page * limit)
            {
                limit = count - (page - 1) * limit;
            }
            page = (page_temp - 1) * limit_temp;
            string sql = "select top " + limit + " a.student_id ,a.student_name ,c.college_name, b.major_name,a.student_sex,a.student_tel,a.student_id_card from tb_student as a "
            +" inner join tb_major as b on a.major_id=b.major_id "
            +"inner join tb_college as c on b.college_id = c.college_id";
            
            if(wheres.Count > 0)
            {
                sql = sql + " where " + wh + " and student_id not in (select top " + page + "  student_id from tb_student " + " where " + wh+")";
            }
            else
            {
               sql=sql + " where student_id not in (select top " + page + "  student_id from tb_student)";
            }
            table1 = SqlServer.GetDataTable(sql);
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('有错误')</script>");
            Response.End();
        }
        string a = Json.ToJson(table1, count);
        Response.Write(Json.ToJson(table1, count));
        Response.End();

    }
    

}